package com.gton.config;

import cn.dev33.satoken.stp.StpInterface;
import com.gton.entity.AuthLoginUser;
import com.gton.service.AuthLoginUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @description: 获取当前账号权限码集合
 * @author: GuoTong
 * @createTime: 2022-11-29 20:24
 * @since JDK 1.8 OR 11
 **/
@Component
@Slf4j
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private AuthLoginUserService authLoginUserService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        AuthLoginUser user = null;
        try {
            user = authLoginUserService.getById(loginId.toString());
        } catch (Exception e) {
            log.info("Id无效--->{}", loginId);
        }
        assert user != null;
        String rule = user.getRule();
        return Collections.singletonList(rule);
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        AuthLoginUser user = null;
        try {
            user = authLoginUserService.getById(loginId.toString());
        } catch (Exception e) {
            log.info("Id无效--->{}", loginId);
        }
        assert user != null;
        String rule = user.getRule();
        return Collections.singletonList(rule);
    }

}